﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace AppDataStruct
{
    public class XmlExplain
    {
        /// <summary>
        /// xml文件转成PreApplyInfo类
        /// </summary>
        /// <param name="xmlFullName">xml文件全路径</param>
        /// <param name="encoding">xml编码方式（若为null则使用默认编码default）</param>
        /// <returns></returns>
        public PreApplyInfo XmlFileToClass(string xmlFullName,Encoding encoding)
        {
            try
            {
                if (!File.Exists(xmlFullName))
                {
                    throw new Exception(string.Format("XML文件：{0} 不存在，请确认路径正确", xmlFullName));
                }
                Encoding encod = encoding == null ? Encoding.Default : encoding;
                string content = File.ReadAllText(xmlFullName, encod);
                PreApplyInfo appInfo = XmlStringToClass(content);
                return appInfo;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// xml格式的字符串转成PreApplyInfo类
        /// </summary>
        /// <param name="xmlString">xml格式的字符串</param>
        /// <returns></returns>
        public PreApplyInfo XmlStringToClass(string xmlString)
        {
            try
            {
                if (string.IsNullOrEmpty(xmlString))
                {
                    throw new Exception("XML字符串为空");
                }

                PreApplyInfo appInfo = SerialiazeHelper.FromXml<PreApplyInfo>(xmlString);

                return appInfo;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// PreApplyInfo类转换成xml字符串
        /// </summary>
        /// <param name="appInfo">PreApplyInfo对象实例</param>
        /// <returns></returns>
        public string ClassToXmlString(PreApplyInfo appInfo)
        {
            try
            {
                if (appInfo == null)
                {
                    throw new Exception("传入的参数为NULL");
                }
                string content = SerialiazeHelper.ToXml<PreApplyInfo>(appInfo,true);

                return content;
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
        }

        /// <summary>
        /// PreApplyInfo类转换成xml文件(文件名按日期自动生成)
        /// </summary>
        /// <param name="appInfo">PreApplyInfo对象实例</param>
        public void ClassToXmlFile(PreApplyInfo appInfo)
        {
            try
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory + string.Format("{0}.xml",DateTime.Now.ToString("yyyyMMddHHmmssfff"));
                string content = ClassToXmlString(appInfo);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(content);
                doc.Save(filePath);
            }
            catch (Exception ex)
            {
                
                throw ex;
            }
           
        }

        /// <summary>
        /// PreApplyInfo类转换成xml文件
        /// </summary>
        /// <param name="appInfo">PreApplyInfo对象实例</param>
        /// <param name="fileName">转成后的文件名</param>
        public void ClassToXmlFile(PreApplyInfo appInfo,string fileName)
        {
            try
            {
                //string filePath = AppDomain.CurrentDomain.BaseDirectory + fileName;
                string filePath = fileName;
                string content = ClassToXmlString(appInfo);
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(content);
                doc.Save(filePath);
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }


    }
}
